package com.project.filter;

import com.project.bean.UserBean;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author lyl
 * description:
 */
public class ManagerLoginFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //强制转换响应
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        //取出登录用户存放在session中的登录用户
        UserBean userBean = (UserBean) request.getSession().getAttribute("userBean");
        if (userBean == null) {
            //等于null时候跳转至登录界面
            response.sendRedirect("/html/login.html?errorInfo=noLand");
        } else if (userBean.getLevel().equals("管理员")) {
            filterChain.doFilter(request, response);
        } else {
            //不是管理员对象时
            response.sendRedirect("/html/login.html?errorInfo=errorLevel");
        }
    }
}
